package com.ms.game.bank.db;

import java.math.BigDecimal;

import com.ms.game.bank.message.BankResponse;
import com.ms.game.lobby.GameLobbyModule;
import com.ms.game.password.db.PasswordDisableTime;
import com.ms.game.password.db.PasswordErrorWay;
import com.ms.game.password.db.PasswordLogModel;
import com.ms.player.Player;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.account.Account;
import com.ms.service.module.account.db.AccountDAO;
import com.ms.service.module.datacenterlite.CentralAsyncCommand;
import com.xpec.c4.db.dao.PlayerDAO;
import com.xpec.c4.db.model.PlayerModel;
import com.xpec.c4.game.common.Helper;
import com.xpec.c4.game.datamanager.agent.money.ChangeCoinLister;
import com.xpec.c4.game.datamanager.agent.money.CoinType;
import com.xpec.c4.game.datamanager.agent.money.MoneyChangeParam;
import com.xpec.c4.game.datamanager.agent.money.MoneyChangeWay;
import com.xpec.c4.game.time.Stamper;
import com.xpec.c4.game.time.TimeSys;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.game.utility.GUIDFactory;
import com.xpec.c4.game.utility.GUIDType;
import com.xpec.c4.service.message.MessageAction;

public class checkPasswordDCLAsync extends CentralAsyncCommand {

	private static final long serialVersionUID = 2966996830201190554L;

	public GUID accountID;
	
	public GUID playerID;
	
	public String playName;
	
	public GUID sessionID;
	
	public String password;
	
	/** 密碼錯誤時紀錄原因 */
	public PasswordErrorWay errorWay; 
	
	private String accountPassword;
	
	/** 錯誤的次數 */
	private int errorTimes;
	
	/** 超過5次後的間隔時間 */
	private long disableTimes;
	
	/** 解密的密碼*/
	private String reductionPassword ;
	@Override
	public void asyncExecute() throws Exception {
		
		Account accountData = AccountDAO.loadAccountByAccountId(accountID);
		reductionPassword = Stamper.getInstance().Reduction(accountData.getBankPassword());
		if (reductionPassword != null) {
			//解密成功
			accountPassword = reductionPassword;

			PlayerModel model = PlayerDAO.getErrorTimesAndDisableTimeByGuid(playerID);
			disableTimes = model.getDisableTime();
			errorTimes = model.getErrorTimes();

			if (disableTimes > TimeSys.currentTimeMills()) {
				return;
			}

			if (accountPassword != null) {
				if (!accountPassword.equals(password)) {
					// 輸入錯誤紀錄
					model.setErrorTimes(errorTimes + 1);
					errorTimes = model.getErrorTimes();
					if (model.getErrorTimes() == 5) {
						model.setDisableTime(TimeSys.currentTimeMills() + PasswordDisableTime.BANK_FIVE_TIMES_DISABLE_TIME);
						disableTimes = model.getDisableTime();
					} else if (model.getErrorTimes() >= 10 && (model.getErrorTimes() % 5) == 0) {
						model.setDisableTime(TimeSys.currentTimeMills() + PasswordDisableTime.BANK_TEN_TIMES_DISABLE_TIME);
						disableTimes = model.getDisableTime();
					}

					PasswordLogModel log = new PasswordLogModel();
					log.setOrderId(GUIDFactory.createUUID(GUIDType.TYPE_BANK_MONEY).toString());
					log.setAccountId(accountID.toString());
					log.setPlayerId(playerID.toString());
					log.setPlayerName(playName);
					log.setPasswordEnter(password);
					log.setErrorTimes(model.getErrorTimes());
					log.setChangeWay(errorWay.getId());
					log.setChangeWayDesc(errorWay.getDesc());
					getCurrentModuleServer().saveDbLog(log);

					PlayerDAO.updatePlayerModelPasswordError(model);
				} else {
					// 輸入正確歸0
					model.setErrorTimes(0);
					model.setDisableTime(0);
					model.setErrorTimesGift(0);
					model.setDisableTimeGift(0);
					PlayerDAO.updatePlayerModelPasswordError(model);
				}
			}
		}
	}

	@Override
	public void callBackExecute(Object... userData) throws Exception {
		
		GameLobbyModule module = (GameLobbyModule)userData[0];
		Player playerData = (Player)userData[1];
		MoneyChangeWay moneyChangeWay = (MoneyChangeWay)userData[2];
		long goldCoin = (long)userData[3];
		int messageType = (int)userData[4];
		
		BigDecimal goldCoinBig = new BigDecimal(goldCoin);
		
		BankResponse response = new BankResponse(); 
		
		if(reductionPassword == null){
			response.errorCode = ErrorCode.DECRYPTION_FAILED;
			MessageAction.sendMessage(sessionID, messageType, response);
			return;
		}
		
		//密碼判斷
		if(accountPassword == "" || accountPassword == null){
			//尚未建立密碼
			response.errorCode = ErrorCode.PASSWORD_CREATE_NOT_YET;
			MessageAction.sendMessage(sessionID, messageType, response);
			return;
		}
		
		if(!accountPassword.equals(password)){
			
			Player playerSave= module.getZone().getPlayer(playerID);
			//如果在線上
			if(playerSave != null){
				//寫回zone player
				playerSave.setErrorTimes(errorTimes);
				playerSave.setDisableTime(disableTimes);
			}
			
			if(disableTimes > TimeSys.currentTimeMills()){
				//錯誤太多次時間內不能操作
				response.disableTime = disableTimes;
				response.errorCode = ErrorCode.PASSWORD_ERROR_DISABLE_TIME;
				MessageAction.sendMessage(sessionID, messageType, response);
				return;
			}
			
			//密碼錯誤
			response.errorCode = ErrorCode.PASSWORD_ERROR;
			response.errorTimes = errorTimes + 1;
			MessageAction.sendMessage(sessionID, messageType, response);
			return;
		}
		
		if(moneyChangeWay == MoneyChangeWay.SUB_BANK_DEPOSIT){
			if(Helper.isLess(playerData.getGoldCoin(), goldCoinBig)){
				//金錢不足
				response.errorCode = ErrorCode.MONEY_NOT_ENOUGH;
				MessageAction.sendMessage(sessionID, messageType, response);
				return;
			}
		}else{
			if(playerData.getBankGoldCoinLongValue() < goldCoin){
				//金錢不足
				response.errorCode = ErrorCode.MONEY_NOT_ENOUGH;
				MessageAction.sendMessage(sessionID, messageType, response);
				return;
			}
		}
		
		ChangeCoinLister coinLister = new ChangeCoinLister();
		coinLister.addCoin(CoinType.GOLD, goldCoinBig);
		//金錢寫回DC，給自己
		module.getPlayerDataAgent().updatePlayerMoneyToDC(GUIDFactory.createUUID(GUIDType.TYPE_BANK_MONEY),
				playerData, coinLister, moneyChangeWay, MoneyChangeParam.NO_JACKPOT, false, null, null, module,
				null, null, null, null, null, module.getBank(), "bankChangeMoneyCallback", sessionID, messageType,
				module);

	}

}
